package com.scitc.pleasurevideo.util;

import android.content.Context;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.scitc.pleasurevideo.pojo.PlayMenuTvVo;

import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

public class GetHtmlContentUtil {
    private static GetPlayMenuTvList getPlayMenuTvList;

    public interface GetPlayMenuTvList {
        void playmenuTvList(List<PlayMenuTvVo> playMenuTvPojoList);

        void playmenuTv(PlayMenuTvVo playMenuTvPojo);
    }

    public static void getPlayMenuTvList(GetPlayMenuTvList menuTvList) {
        getPlayMenuTvList = menuTvList;
    }

    public static void getPlayMenuTvList(final Context context, final String url) {
        OkGo.<String>get(url)
                .tag(context)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Elements elements = HTMLContentUtil.getElementsByClassName(response.body(), "vodplayinfo");
                        if (elements == null || elements.size() < 1) {
                            return;
                        }
                        List<PlayMenuTvVo> playMenuTvPojoList = new ArrayList<>();
                        for (int i = 0; i < elements.size(); i++) {
                            Elements uls = elements.get(i).getElementsByTag("ul");
                            if (uls == null || uls.size() < 1) {
                                continue;
                            }
                            Elements inputs = uls.get(0).getElementsByTag("input");
                            if (inputs == null || inputs.size() < 1) {
                                continue;
                            }
                            for (int j = 0; j < uls.size(); j++) {
                                Elements lis = uls.get(j).getElementsByTag("li");
                                if (lis == null || lis.size() < 1) {
                                    break;
                                }
                                for (int z = 0; z < lis.size(); z++) {
                                    String tempString = lis.get(z).text();
                                    String[] splitstr = tempString.split("\\$");

                                    if (playMenuTvPojoList.size() <= z) {
                                        PlayMenuTvVo menuTvPojo = new PlayMenuTvVo();
                                        menuTvPojo.setName(splitstr[0]);
                                        if (splitstr[1].indexOf(".m3u8") > 0) {
                                            menuTvPojo.setPlayM3u8Url(splitstr[1]);
                                        } else if (splitstr[1].indexOf(".mp4") > 0) {
                                            menuTvPojo.setPlayMp4Url(splitstr[1]);
                                        } else {
                                            menuTvPojo.setPlayWebUrl(splitstr[1]);
                                        }
                                        playMenuTvPojoList.add(menuTvPojo);
                                    } else {
                                        if (splitstr[1].indexOf(".m3u8") > 0) {
                                            playMenuTvPojoList.get(z).setPlayM3u8Url(splitstr[1]);
                                        } else if (splitstr[1].indexOf(".mp4") > 0) {
                                            playMenuTvPojoList.get(z).setPlayMp4Url(splitstr[1]);
                                        } else {
                                            playMenuTvPojoList.get(z).setPlayWebUrl(splitstr[1]);
                                        }
                                    }

                                }
                            }
                        }

                        for (int i = 1; i < elements.size(); i++) {
                            if (elements.get(i).html().indexOf("m3u8") > 0) {
                                playMenuTvPojoList.get(0).setSubtitle("简介：" + elements.get(i - 1).text());
                            }
                        }

                        Elements elements1 = HTMLContentUtil.getElementsByClassName(response.body(), "vodinfobox");
                        if (!(elements == null || elements.size() < 1)) {
                            Elements elementsLi = elements1.get(0).getElementsByTag("li");
                            if (elementsLi == null || elementsLi.size() < 1) {
                                return;
                            }
                            List<String> videoInformation = new ArrayList<>();
                            for (int i = 0; i < elementsLi.size(); i++) {
                                videoInformation.add(elementsLi.get(i).text());
                            }
                            for (int i = 0; i < videoInformation.size(); i++) {
//                            Log.e("GetHtmlContentUtil", videoInformation.get(i));
                                if (videoInformation.get(i).contains("导演")) {
                                    playMenuTvPojoList.get(0).setDirector(videoInformation.get(i));
                                } else if (videoInformation.get(i).contains("主演")) {
                                    playMenuTvPojoList.get(0).setProtagonist(videoInformation.get(i));
                                }
                            }
                        }

                        getPlayMenuTvList.playmenuTvList(playMenuTvPojoList);
                    }
                });
    }

}
